#!/bin/sh
# Written at 2015.06.15 by Kevin 
# Included command : start, stop, restart, status

this_dir=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
    this_dir=`dirname $0`
else
    dirname $0|grep "^\." >/dev/null
    retval=$?
    if [ $retval -eq 0 ];then
        this_dir=`dirname $0|sed "s#^.#$this_dir#"`
    else
        this_dir=`dirname $0|sed "s#^#$this_dir/#"`
    fi
fi

# echo $this_dir

pidfile=${this_dir}/logs/mypid

stop_timeout=30

start() {
    MEMORY="-Xms512M -Xmx512M -XX:-HeapDumpOnOutOfMemoryError"
    XXARGS="-Xmn300m -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=4 -XX:+PrintGC"
    ENCODE="-Dfile.encoding=UTF-8 -Dclient.encoding.override=UTF-8"
    JMXARGS="-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9991 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.net.preferIPv4Stack=true"
    VMARGS="$ENCODE $MEMORY"
    logfie=`date +%Y%m%d.log`
    nohup java -server $VMARGS -Dkeel.home=${this_dir} -Djava.library.path=${this_dir} -jar ${this_dir}/lib/keel-core-loader-1.0.0.jar startup > ${this_dir}/logs/$logfie 2>&1 &
    sleep 1
    echo -n $"Starting soa server: "
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        echo "successfully"
    else
        echo "failed"
    fi
}

stop() {
    echo -n $"Stopping soa server: "
    processpid=0
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        java -Dkeel.home=${this_dir} -jar ${this_dir}/lib/keel-core-loader-1.0.0.jar shutdown
        sleep 2
    fi
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        echo "failed"
    else
        echo "successfully"
    fi
}

restart() {
    stop
    start
}

status() {
    echo -n $"Soa server: "
    processpid=0
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
		echo "running"
    else
        echo "down"
    fi
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit $?
